import { DefaultStage } from "../default-stage";
import { Global } from '../../data/data';
import { HUB_SCOPE, MessageHub } from "../../../../common/global/message-hub";
import { ISetupData, EDIT_HUB_MESSAGE_TYPE } from "../../../../common/global/edit-hub-message-type";
import { GalleryResolver } from "../../../../common/assets/gallery-resolver/gallery-resolver";
import { PhysicsManager } from "../../../../common/physics/physicsManager";
import { CharacterController, CONTROLLER_TYPE } from "../../../../common/interactive-object/character/character-controller/character-controller";
import { ArcRotateCamera, Vector3 } from "babylon-lib/index";
import { VirtualJoystick } from "../../../../common/interactive-object/character/character-controller/virtual-joystick";
import { RawAssetsUrl } from "../../raw-assets-info";
import { Character } from '../../../../common/interactive-object/character/character';
import { MultiplayerManager } from "../../../../common/multiplayer/client/multiplayer-manager";
import { CharacterManager } from '../../../../common/multiplayer/client/character-manager';


class InitializeHub {

    public static initialize(stage: DefaultStage): void {
        Global.hub = new MessageHub<EDIT_HUB_MESSAGE_TYPE>(window.parent, HUB_SCOPE.EDIT);
        this.setup(stage);
        Global.hub.dispatch(EDIT_HUB_MESSAGE_TYPE.IFRAME_READY, undefined);
    }

    private static setup(stage: DefaultStage): void {
        Global.hub!.listen(EDIT_HUB_MESSAGE_TYPE.SETUP, (_data) => {
            const data = JSON.parse(_data.data as unknown as string) as ISetupData;
            console.log('edit setup with data', data);

            CharacterManager.initialize(stage);

            const resolve = new GalleryResolver(data.scene, stage, async (r) => {
                PhysicsManager.debug = true;
                const controller = new CharacterController(stage.mainCamera as ArcRotateCamera, stage, {
                    mode: CONTROLLER_TYPE.THIRD_PERSON,
                    // disablePhysicalEffects: true,
                    // hideCharacter:true,
                });
                CharacterManager.defineController(controller);
                if (VirtualJoystick.initialized) {
                    VirtualJoystick.fromTexture = stage.loader(stage.mainScene).getTextureTaskInfo(RawAssetsUrl.textureCircle)?.texture!;
                    VirtualJoystick.toTexture = stage.loader(stage.mainScene).getTextureTaskInfo(RawAssetsUrl.textureRound)?.texture!;
                }

                const character = await Character.create(stage, {
                    id: data.user.id,
                    name: data.user.name,
                    modelUrl: data.user.outfit,
                    idleIndex: 5,
                    walkIndex: 6,
                    
                });
                controller.setCharacter(character, new Vector3(0, -1, 0));
                CharacterManager.defineSelf(character);
                character.playAction(character.idleIndex, true);

                controller.debug = false;

                setTimeout(() => {
                    character.isVisible = false;
                }, 1000);
                setTimeout(() => {
                    character.isVisible = true;
                }, 3000);
            });

        });
    }

}


export { InitializeHub };